﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using System.Transactions;
using System.Text;
using HKEA.CMS.Core.BLL.Tbl;
using HKEA.CMS.Core.BLL.Mtbl;

namespace WebApplication
{
    public partial class AddressContactInformation : System.Web.UI.Page
    {
        static tblAddress staticeEnt;
        static tblContactInfo statictEntInfo=new tblContactInfo();


        tblCustomer customerENT = new tblCustomer();
        tblCustomerBLL customerBLL = new tblCustomerBLL();
        tblAddress addressENT = new tblAddress();
        tblAddressBLL addressBLL = new tblAddressBLL();
        tblContactInfo infoENT = new tblContactInfo();
        tblContactInfoBLL infoBLL = new tblContactInfoBLL();
        int telNum = 5;
        int faxNum = 5;
        int emailNum = 5;
        int webNum = 2;
        protected int customeruid = 0;
        int addressUID = 0;
        int infoUID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString[Constant.UrlParamName.CustomerUID] != null)
                {
                    if (int.TryParse(Request.QueryString[Constant.UrlParamName.CustomerUID], out customeruid))
                    {
                        InitPage();
                    }
                    else
                    {
                        SetNoOption();
                        lblMeg.Text = ResourceManage.GetResourceString("msg200001");
                    }
                }
                else
                {
                    SetNoOption();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200002");
                }
            }
        }
        #region 页面加载
        private void InitPage()
        {
            customerENT.CustomerUID = customeruid;
            customerENT = customerBLL.GetEntity(customerENT);
            if (customerENT != null)
            {
                hidCustomerUID.Value = customerENT.CustomerUID.ToString();
                AddToDropList();
                BindArea();
                SetAddress(customerENT.AddressUID);
                SetInfo(customerENT.ContactInfoUID);
            }
            else
            {
                SetNoOption();
                lblMeg.Text = ResourceManage.GetResourceString("msg200014");
            }

        }
        protected void SetNoOption()
        {
            initInfo();
            hidAllowToOption.Value = "false";
            lkBtnSave.Enabled = false;
        }
        #endregion

        #region 页面加载给文本赋值
        private void SetAddress(int addressUID)
        {
            staticeEnt = new tblAddress();
            addressENT.AddressUID = addressUID;
            addressENT = addressBLL.GetEntity(addressENT);
            addressENT = staticeEnt;
            if (addressENT != null)
            {
                hidAddressUID.Value = addressENT.AddressUID.ToString();
                txtAreaChi.Text = addressENT.Area_Chi;
                txtAreaEng.Text = addressENT.Area_Eng;
                txtCityChi.Text = addressENT.City_Chi;
                txtCityEng.Text = addressENT.City_Eng;
                txtDistrictChi.Text = addressENT.District_Chi;
                txtDistrictEng.Text = addressENT.District_Eng;
                txtFlatEng.Text = addressENT.Flat_Bldg_Eng;
                txtFlatChi.Text = addressENT.Flat_Bldg_Chi;
                txtProvinceChi.Text = addressENT.Province_Chi;
                txtProvinceEng.Text = addressENT.Province_Eng;
                txtStreetChi.Text = addressENT.Street_Rd_Chi;
                txtStreetEng.Text = addressENT.Street_Rd_Eng;
                txtPostCode.Text = addressENT.PostalCode;
                if (addressENT.CountryUID != null)
                {
                    if (addressENT.mtblCountry.AreaUID != null)
                    {
                        ddlArea.SelectedValue = addressENT.mtblCountry.AreaUID.ToString();
                    }
                    BindCountry();
                    ddlCountry.SelectedValue = addressENT.CountryUID.ToString();
                }
            }
        }
        private void SetInfo(int infoUID)
        {
            infoENT.ContactInfoUID = infoUID;
            infoENT = infoBLL.GetEntity(infoENT);
            #region 
                statictEntInfo = infoENT;
            #endregion
            if (infoENT != null)
            {
                hidInfoUID.Value = infoENT.ContactInfoUID.ToString();
                #region Info
                DropDownListTel1.SelectedValue = infoENT.Tel1_Type.ToString();
                TextBoxTC1.Text = infoENT.Tel1_CountryCode;
                TextBoxTA1.Text = infoENT.Tel1_AreaCode;
                TextBoxT1.Text = infoENT.Tel1;
                CheckBoxTD1.Checked = infoENT.Tel1_IsDNC == 1 ? true : false;
                CheckBoxTS1.Checked = infoENT.Tel1_IsSubscribe == 1 ? true : false;

                if (string.IsNullOrEmpty(infoENT.Tel1_CountryCode) && string.IsNullOrEmpty(infoENT.Tel1_AreaCode) && string.IsNullOrEmpty(infoENT.Tel1))
                {
                    PanelT1.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel2.SelectedValue = infoENT.Tel2_Type.ToString();
                TextBoxTC2.Text = infoENT.Tel2_CountryCode;
                TextBoxTA2.Text = infoENT.Tel2_AreaCode;
                TextBoxT2.Text = infoENT.Tel2;
                CheckBoxTD2.Checked = infoENT.Tel2_IsDNC == 1 ? true : false;
                CheckBoxTS2.Checked = infoENT.Tel2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Tel2_CountryCode) && string.IsNullOrEmpty(infoENT.Tel2_AreaCode) && string.IsNullOrEmpty(infoENT.Tel2))
                {
                    PanelT2.Style.Add("display", "none");
                    telNum--;
                }



                DropDownListTel3.SelectedValue = infoENT.Tel3_Type.ToString();
                TextBoxTC3.Text = infoENT.Tel3_CountryCode;
                TextBoxTA3.Text = infoENT.Tel3_AreaCode;
                TextBoxT3.Text = infoENT.Tel3;
                CheckBoxTD3.Checked = infoENT.Tel3_IsDNC == 1 ? true : false;
                CheckBoxTS3.Checked = infoENT.Tel3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Tel3_CountryCode) && string.IsNullOrEmpty(infoENT.Tel3_AreaCode) && string.IsNullOrEmpty(infoENT.Tel3))
                {
                    PanelT3.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel4.SelectedValue = infoENT.Tel4_Type.ToString();
                TextBoxTC4.Text = infoENT.Tel4_CountryCode;
                TextBoxTA4.Text = infoENT.Tel4_AreaCode;
                TextBoxT4.Text = infoENT.Tel4;
                CheckBoxTD4.Checked = infoENT.Tel4_IsDNC == 1 ? true : false;
                CheckBoxTS4.Checked = infoENT.Tel4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Tel4_CountryCode) && string.IsNullOrEmpty(infoENT.Tel4_AreaCode) && string.IsNullOrEmpty(infoENT.Tel4))
                {
                    PanelT4.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel5.SelectedValue = infoENT.Tel5_Type.ToString();
                TextBoxTC5.Text = infoENT.Tel5_CountryCode;
                TextBoxTA5.Text = infoENT.Tel5_AreaCode;
                TextBoxT5.Text = infoENT.Tel5;
                CheckBoxTD5.Checked = infoENT.Tel5_IsDNC == 1 ? true : false;
                CheckBoxTS5.Checked = infoENT.Tel5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Tel5_CountryCode) && string.IsNullOrEmpty(infoENT.Tel5_AreaCode) && string.IsNullOrEmpty(infoENT.Tel5))
                {
                    PanelT5.Style.Add("display", "none");
                    telNum--;
                }
                if (telNum <= 0)
                    PanelT5.Style.Add("display", "true");
                #endregion

                #region Fax
                //Fax
                DropDownListFax1.SelectedValue = infoENT.Fax1_Type.ToString();
                TextBoxFC1.Text = infoENT.Fax1_CountryCode;
                TextBoxFA1.Text = infoENT.Fax1_AreaCode;
                TextBoxF1.Text = infoENT.Fax1;
                CheckBoxFD1.Checked = infoENT.Fax1_IsDNC == 1 ? true : false;
                CheckBoxFS1.Checked = infoENT.Fax1_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Fax1) && string.IsNullOrEmpty(infoENT.Fax1_AreaCode) && string.IsNullOrEmpty(infoENT.Fax2_CountryCode))
                {
                    PanelF1.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax2.SelectedValue = infoENT.Fax2_Type.ToString();
                TextBoxFC2.Text = infoENT.Fax2_CountryCode;
                TextBoxFA2.Text = infoENT.Fax2_AreaCode;
                TextBoxF2.Text = infoENT.Fax2;
                CheckBoxFD2.Checked = infoENT.Fax2_IsDNC == 1 ? true : false;
                CheckBoxFS2.Checked = infoENT.Fax2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Fax2) && string.IsNullOrEmpty(infoENT.Fax2_AreaCode) && string.IsNullOrEmpty(infoENT.Fax2_CountryCode))
                {
                    PanelF2.Style.Add("display", "none");
                    faxNum--;
                }




                DropDownListFax3.SelectedValue = infoENT.Fax3_Type.ToString();
                TextBoxFC3.Text = infoENT.Fax3_CountryCode;
                TextBoxFA3.Text = infoENT.Fax3_AreaCode;
                TextBoxF3.Text = infoENT.Fax3;
                CheckBoxFD3.Checked = infoENT.Fax3_IsDNC == 1 ? true : false;
                CheckBoxFS3.Checked = infoENT.Fax3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Fax3) && string.IsNullOrEmpty(infoENT.Fax3_AreaCode) && string.IsNullOrEmpty(infoENT.Fax3_CountryCode))
                {
                    PanelF3.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax4.SelectedValue = infoENT.Fax4_Type.ToString();
                TextBoxFC4.Text = infoENT.Fax4_CountryCode;
                TextBoxFA4.Text = infoENT.Fax4_AreaCode;
                TextBoxF4.Text = infoENT.Fax4;
                CheckBoxFD4.Checked = infoENT.Fax4_IsDNC == 1 ? true : false;
                CheckBoxFS4.Checked = infoENT.Fax4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Fax4) && string.IsNullOrEmpty(infoENT.Fax4_AreaCode) && string.IsNullOrEmpty(infoENT.Fax4_CountryCode))
                {
                    PanelF4.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax5.SelectedValue = infoENT.Fax5_Type.ToString();
                TextBoxFC5.Text = infoENT.Fax5_CountryCode;
                TextBoxFA5.Text = infoENT.Fax5_AreaCode;
                TextBoxF5.Text = infoENT.Fax5;
                CheckBoxFD5.Checked = infoENT.Fax5_IsDNC == 1 ? true : false;
                CheckBoxFS5.Checked = infoENT.Fax5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Fax5) && string.IsNullOrEmpty(infoENT.Fax5_AreaCode) && string.IsNullOrEmpty(infoENT.Fax5_CountryCode))
                {
                    PanelF5.Style.Add("display", "none");
                    faxNum--;
                }
                if (faxNum <= 0)
                    PanelF1.Style.Add("display", "true");
                #endregion
                #region Email
                //email 
                DropDownListEm1.SelectedValue = infoENT.Email1_Type.ToString();
                TextBoxEmail1.Text = infoENT.Email1;
                CheckBoxES1.Checked = infoENT.Email1_IsSubscribe == 1 ? true : false;
                CheckBoxHB1.Checked = infoENT.Email1_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Email1))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }



                DropDownListEm2.SelectedValue = infoENT.Email2_Type.ToString();
                TextBoxEmail2.Text = infoENT.Email2;
                CheckBoxES2.Checked = infoENT.Email2_IsSubscribe == 1 ? true : false;
                CheckBoxHB2.Checked = infoENT.Email2_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Email2))
                {
                    PanelE2.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm3.SelectedValue = infoENT.Email3_Type.ToString();
                TextBoxEmail3.Text = infoENT.Email3;
                CheckBoxES3.Checked = infoENT.Email3_IsSubscribe == 1 ? true : false;
                CheckBoxHB3.Checked = infoENT.Email3_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Email3))
                {
                    PanelE3.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm4.SelectedValue = infoENT.Email4_Type.ToString();
                TextBoxEmail4.Text = infoENT.Email4;
                CheckBoxES4.Checked = infoENT.Email4_IsSubscribe == 1 ? true : false;
                CheckBoxHB4.Checked = infoENT.Email4_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Email4))
                {
                    PanelE4.Style.Add("display", "none");
                    emailNum--;
                }


                DropDownListEm5.SelectedValue = infoENT.Email5_Type.ToString();
                TextBoxEmail5.Text = infoENT.Email5;
                CheckBoxES5.Checked = infoENT.Email5_IsSubscribe == 1 ? true : false;
                CheckBoxHB5.Checked = infoENT.Email5_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(infoENT.Email5))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }
                if (emailNum <= 0)
                    PanelE1.Style.Add("display", "true");
                #endregion
                #region WebSite
                //website
                DropDownListW1.SelectedValue = infoENT.Website1_Type.ToString();
                TextBoxWeb1.Text = infoENT.Website1;
                if (string.IsNullOrEmpty(infoENT.Website2))
                {
                    PanelW1.Style.Add("display", "none");
                    webNum--;
                }
                DropDownListW2.SelectedValue = infoENT.Website2_Type.ToString();
                TextBoxWeb2.Text = infoENT.Website2;
                if (string.IsNullOrEmpty(infoENT.Website2))
                {
                    PanelW2.Style.Add("display", "none");
                    webNum--;
                }
                if (webNum <= 0)
                    PanelW1.Style.Add("display", "true");
                #endregion
            }
            else
            {
                initInfo();
            }
        }
        #endregion

        #region 页面加载初始化控件
        protected void BindArea()
        {
            mtblArea areaENT = new mtblArea();
            areaENT.Deleted = 0;
            mtblAreaBLL areaBLL = new mtblAreaBLL();
            var list = areaBLL.GetListByENT(areaENT);
            ddlArea.DataSource = list;
            ddlArea.DataTextField = "Area_Chi";
            ddlArea.DataValueField = "AreaUID";
            ddlArea.DataBind();
            BindCountry();
        }
        protected void BindCountry()
        {
            mtblCountry countryENT = new mtblCountry();
            mtblCountryBLL countryBLL = new mtblCountryBLL();
            countryENT.Deleted = 0;
            int areaUID = Convert.ToInt32(ddlArea.SelectedValue);
            countryENT.AreaUID = areaUID;
            var list = countryBLL.GetListByENT(countryENT);
            if (list.Count > 0)
            {
                ddlCountry.DataSource = list;
                ddlCountry.DataTextField = "Country_Chi";
                ddlCountry.DataValueField = "CountryUID";
                ddlCountry.DataBind();
            }
        }
        private void AddToDropList()
        {
            Hashtable tellHas = Constant.InfoType.tellType;
            Hashtable FaxHas = Constant.InfoType.FaxType;
            Hashtable MailHas = Constant.InfoType.EmailType;
            Hashtable WebHas = Constant.InfoType.WebType;
            //telphone
            DropDownListTel1.DataSource = DropDownListTel2.DataSource = DropDownListTel3.DataSource = DropDownListTel4.DataSource = DropDownListTel5.DataSource = tellHas;
            DropDownListTel1.DataTextField = DropDownListTel2.DataTextField = DropDownListTel3.DataTextField = DropDownListTel4.DataTextField = DropDownListTel5.DataTextField = "value";
            DropDownListTel1.DataValueField = DropDownListTel2.DataValueField = DropDownListTel3.DataValueField = DropDownListTel4.DataValueField = DropDownListTel5.DataValueField = "key";
            DropDownListTel1.DataBind();
            DropDownListTel2.DataBind();
            DropDownListTel3.DataBind();
            DropDownListTel4.DataBind();
            DropDownListTel5.DataBind();
            //Fax
            DropDownListFax1.DataSource = DropDownListFax2.DataSource = DropDownListFax3.DataSource = DropDownListFax4.DataSource = DropDownListFax5.DataSource = FaxHas;
            DropDownListFax1.DataTextField = DropDownListFax2.DataTextField = DropDownListFax3.DataTextField = DropDownListFax4.DataTextField = DropDownListFax5.DataTextField = "value";
            DropDownListFax1.DataValueField = DropDownListFax2.DataValueField = DropDownListFax3.DataValueField = DropDownListFax4.DataValueField = DropDownListFax5.DataValueField = "key";
            DropDownListFax1.DataBind();
            DropDownListFax2.DataBind();
            DropDownListFax3.DataBind();
            DropDownListFax4.DataBind();
            DropDownListFax5.DataBind();
            //Mail
            DropDownListEm1.DataSource = DropDownListEm2.DataSource = DropDownListEm3.DataSource = DropDownListEm4.DataSource = DropDownListEm5.DataSource = MailHas;
            DropDownListEm1.DataTextField = DropDownListEm2.DataTextField = DropDownListEm3.DataTextField = DropDownListEm4.DataTextField = DropDownListEm5.DataTextField = "value";
            DropDownListEm1.DataValueField = DropDownListEm2.DataValueField = DropDownListEm3.DataValueField = DropDownListEm4.DataValueField = DropDownListEm5.DataValueField = "key";
            DropDownListEm1.DataBind();
            DropDownListEm2.DataBind();
            DropDownListEm3.DataBind();
            DropDownListEm4.DataBind();
            DropDownListEm5.DataBind();
            //WEBsit
            DropDownListW1.DataSource = DropDownListW2.DataSource = WebHas;
            DropDownListW1.DataTextField = DropDownListW2.DataTextField = "value";
            DropDownListW1.DataValueField = DropDownListW2.DataValueField = "key";
            DropDownListW1.DataBind();
            DropDownListW2.DataBind();
        }
        /// 新添加时 Contact Information 的样子
        private void initInfo()
        {
            //telephone
            PanelT1.Visible = true;
            PanelT2.Style.Add("display", "none");
            PanelT3.Style.Add("display", "none");
            PanelT4.Style.Add("display", "none");
            PanelT5.Style.Add("display", "none");

            //fax
            PanelF1.Visible = true;
            PanelF2.Style.Add("display", "none");
            PanelF3.Style.Add("display", "none");
            PanelF4.Style.Add("display", "none");
            PanelF5.Style.Add("display", "none");

            //email
            PanelE1.Visible = true;
            PanelE2.Style.Add("display", "none");
            PanelE3.Style.Add("display", "none");
            PanelE4.Style.Add("display", "none");
            PanelE5.Style.Add("display", "none");

            //website
            PanelW1.Visible = true;
            PanelW2.Style.Add("display", "none");


        }
        #endregion

        #region 获取文本值
        private tblAddress GetAddressValue(tblAddress addressENT)
        {
            addressENT.Area_Chi = txtAreaChi.Text;
            addressENT.Area_Eng = txtAreaEng.Text;
            addressENT.City_Chi = txtCityChi.Text;
            addressENT.City_Eng = txtCityEng.Text;
            addressENT.CountryUID = Convert.ToInt32(ddlCountry.SelectedValue);
            addressENT.CreationDate = DateTime.Now;
            addressENT.District_Chi = txtDistrictChi.Text;
            addressENT.District_Eng = txtDistrictEng.Text;
            addressENT.Flat_Bldg_Chi = txtFlatChi.Text;
            addressENT.Flat_Bldg_Eng = txtFlatEng.Text;
            addressENT.LastModifyBy = Common.GetManageName();
            addressENT.LastModifyDate = DateTime.Now;
            addressENT.PostalCode = txtPostCode.Text;
            addressENT.Province_Chi = txtProvinceChi.Text;
            addressENT.Province_Eng = txtProvinceEng.Text;
            addressENT.Street_Rd_Chi = txtStreetChi.Text;
            addressENT.Street_Rd_Eng = txtStreetEng.Text;
            mtblCountry countryENT = new mtblCountry();
            countryENT = addressENT.mtblCountry;
            StringBuilder strBuilderEng = new StringBuilder();
            strBuilderEng.Append(txtFlatEng.Text).Append(txtStreetEng.Text).Append(txtAreaEng.Text).Append(txtDistrictEng.Text).Append(txtCityEng.Text).Append(txtProvinceEng.Text);
            StringBuilder strBuilderChi = new StringBuilder();
            if (countryENT != null)
            {
                strBuilderEng.Append(countryENT.Country_Eng);

                if (countryENT.AreaUID > 0)
                {
                    mtblArea areaENT = new mtblArea();
                    areaENT.AreaUID = (int)countryENT.AreaUID;
                    mtblAreaBLL bll = new mtblAreaBLL();
                    areaENT = bll.GetEntity(areaENT);
                    if (areaENT != null)
                    {
                        strBuilderEng.Append(areaENT.Area_Eng);
                        strBuilderChi.Append(areaENT.Area_Chi);
                    }
                }
                strBuilderChi.Append(countryENT.Country_Chi);
            }
            strBuilderChi.Append(txtProvinceChi.Text).Append(txtCityChi.Text).Append(txtDistrictChi.Text).Append(txtAreaChi.Text).Append(txtStreetChi.Text).Append(txtFlatChi.Text);
            addressENT.FullAdd_Chi = strBuilderChi.ToString();
            addressENT.FullAdd_Eng = strBuilderEng.ToString();
            return addressENT;
        }
        private tblContactInfo GetInfoValue(tblContactInfo infoENT)
        {
            #region Email
            infoENT.Email1 = TextBoxEmail1.Text;
            infoENT.Email1_IsSubscribe = CheckBoxES1.Checked ? 1 : 0;
            infoENT.Email1_Type = Convert.ToInt32(DropDownListEm1.SelectedValue);
            infoENT.Email1_HardBounce = CheckBoxHB1.Checked ? 1 : 0;
            infoENT.Email2 = TextBoxEmail2.Text;
            infoENT.Email2_IsSubscribe = CheckBoxES2.Checked ? 1 : 0;
            infoENT.Email2_Type = Convert.ToInt32(DropDownListEm2.SelectedValue);
            infoENT.Email2_HardBounce = CheckBoxHB2.Checked ? 1 : 0;
            infoENT.Email3 = TextBoxEmail3.Text;
            infoENT.Email3_IsSubscribe = CheckBoxES3.Checked ? 1 : 0;
            infoENT.Email3_Type = Convert.ToInt32(DropDownListEm3.SelectedValue);
            infoENT.Email3_HardBounce = CheckBoxHB3.Checked ? 1 : 0;
            infoENT.Email4 = TextBoxEmail4.Text;
            infoENT.Email4_IsSubscribe = CheckBoxES4.Checked ? 1 : 0;
            infoENT.Email4_Type = Convert.ToInt32(DropDownListEm4.SelectedValue);
            infoENT.Email4_HardBounce = CheckBoxHB4.Checked ? 1 : 0;
            infoENT.Email5 = TextBoxEmail5.Text;
            infoENT.Email5_IsSubscribe = CheckBoxES5.Checked ? 1 : 0;
            infoENT.Email5_Type = Convert.ToInt32(DropDownListEm5.SelectedValue);
            infoENT.Email5_HardBounce = CheckBoxHB5.Checked ? 1 : 0;
            #endregion
            #region Fax
            infoENT.Fax1 = TextBoxF1.Text;
            infoENT.Fax1_AreaCode = TextBoxFA1.Text;
            infoENT.Fax1_CountryCode = TextBoxFC1.Text;
            infoENT.Fax1_IsDNC = CheckBoxFD1.Checked ? 1 : 0;
            infoENT.Fax1_IsSubscribe = CheckBoxFS1.Checked ? 1 : 0;
            infoENT.Fax1_Type = Convert.ToInt32(DropDownListFax1.SelectedValue);

            infoENT.Fax2 = TextBoxF2.Text;
            infoENT.Fax2_AreaCode = TextBoxFA2.Text;
            infoENT.Fax2_CountryCode = TextBoxFC2.Text;
            infoENT.Fax2_IsDNC = CheckBoxFD2.Checked ? 1 : 0;
            infoENT.Fax2_IsSubscribe = CheckBoxFS2.Checked ? 1 : 0;
            infoENT.Fax2_Type = Convert.ToInt32(DropDownListFax2.SelectedValue);

            infoENT.Fax3 = TextBoxF3.Text;
            infoENT.Fax3_AreaCode = TextBoxFA3.Text;
            infoENT.Fax3_CountryCode = TextBoxFC3.Text;
            infoENT.Fax3_IsDNC = CheckBoxFD3.Checked ? 1 : 0;
            infoENT.Fax3_IsSubscribe = CheckBoxFS3.Checked ? 1 : 0;
            infoENT.Fax3_Type = Convert.ToInt32(DropDownListFax3.SelectedValue);

            infoENT.Fax4 = TextBoxF4.Text;
            infoENT.Fax4_AreaCode = TextBoxFA4.Text;
            infoENT.Fax4_CountryCode = TextBoxFC4.Text;
            infoENT.Fax4_IsDNC = CheckBoxFD4.Checked ? 1 : 0;
            infoENT.Fax4_IsSubscribe = CheckBoxFS4.Checked ? 1 : 0;
            infoENT.Fax4_Type = Convert.ToInt32(DropDownListFax4.SelectedValue);

            infoENT.Fax5 = TextBoxF5.Text;
            infoENT.Fax5_AreaCode = TextBoxFA5.Text;
            infoENT.Fax5_CountryCode = TextBoxFC5.Text;
            infoENT.Fax5_IsDNC = CheckBoxFD5.Checked ? 1 : 0;
            infoENT.Fax5_IsSubscribe = CheckBoxFS5.Checked ? 1 : 0;
            infoENT.Fax5_Type = Convert.ToInt32(DropDownListFax5.SelectedValue);
            #endregion
            #region Tel
            infoENT.Tel1 = TextBoxT1.Text;
            infoENT.Tel1_AreaCode = TextBoxTA1.Text;
            infoENT.Tel1_CountryCode = TextBoxTC1.Text;
            infoENT.Tel1_IsDNC = CheckBoxTD1.Checked ? 1 : 0;
            infoENT.Tel1_IsSubscribe = CheckBoxTS1.Checked ? 1 : 0;
            infoENT.Tel1_Type = Convert.ToInt32(DropDownListTel1.SelectedValue);

            infoENT.Tel2 = TextBoxT2.Text;
            infoENT.Tel2_AreaCode = TextBoxTA2.Text;
            infoENT.Tel2_CountryCode = TextBoxTC2.Text;
            infoENT.Tel2_IsDNC = CheckBoxTD2.Checked ? 1 : 0;
            infoENT.Tel2_IsSubscribe = CheckBoxTS2.Checked ? 1 : 0;
            infoENT.Tel2_Type = Convert.ToInt32(DropDownListTel2.SelectedValue);

            infoENT.Tel3 = TextBoxT3.Text;
            infoENT.Tel3_AreaCode = TextBoxTA3.Text;
            infoENT.Tel3_CountryCode = TextBoxTC3.Text;
            infoENT.Tel3_IsDNC = CheckBoxTD3.Checked ? 1 : 0;
            infoENT.Tel3_IsSubscribe = CheckBoxTS3.Checked ? 1 : 0;
            infoENT.Tel3_Type = Convert.ToInt32(DropDownListTel3.SelectedValue);

            infoENT.Tel4 = TextBoxT4.Text;
            infoENT.Tel4_AreaCode = TextBoxTA4.Text;
            infoENT.Tel4_CountryCode = TextBoxTC4.Text;
            infoENT.Tel4_IsDNC = CheckBoxTD4.Checked ? 1 : 0;
            infoENT.Tel4_IsSubscribe = CheckBoxTS4.Checked ? 1 : 0;
            infoENT.Tel4_Type = Convert.ToInt32(DropDownListTel4.SelectedValue);

            infoENT.Tel5 = TextBoxT5.Text;
            infoENT.Tel5_AreaCode = TextBoxTA5.Text;
            infoENT.Tel5_CountryCode = TextBoxTC5.Text;
            infoENT.Tel5_IsDNC = CheckBoxTD5.Checked ? 1 : 0;
            infoENT.Tel5_IsSubscribe = CheckBoxTS5.Checked ? 1 : 0;
            infoENT.Tel5_Type = Convert.ToInt32(DropDownListTel5.SelectedValue);
            #endregion

            #region WebSite
            infoENT.Website1 = TextBoxWeb1.Text;
            infoENT.Website1_Type = Convert.ToInt32(DropDownListW1.SelectedValue);
            infoENT.Website2 = TextBoxWeb2.Text;
            infoENT.Website2_Type = Convert.ToInt32(DropDownListW2.SelectedValue);
            #endregion
            return infoENT;
        }
        #endregion

        #region 按钮事件
        protected void lkBtnSave_Click(object sender, EventArgs e)
        {
            customeruid = Convert.ToInt32(Request.QueryString[Constant.UrlParamName.CustomerUID]);
            addressENT = GetAddressValue(addressENT);
            infoENT = GetInfoValue(infoENT);

            TransactionOptions options = new TransactionOptions();
            options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            options.Timeout = new TimeSpan(0, 3, 0);
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
            {
                bool IsAdd = false;
                //影响行数
                int intCustomer = 0;
                if (!string.IsNullOrEmpty(hidAddressUID.Value))
                {
                    addressENT.AddressUID = Convert.ToInt32(hidAddressUID.Value);
                    addressBLL.UpdateENT(addressENT);
                }
                else
                {
                    IsAdd = true;
                    addressENT.AddressUID = addressBLL.InsertENT(addressENT);
                    hidAddressUID.Value = addressENT.ToString();
                }

                if (!string.IsNullOrEmpty(hidInfoUID.Value))
                {
                    infoENT.ContactInfoUID = Convert.ToInt32(hidInfoUID.Value);
                    infoBLL.UpdateENT(infoENT);
                }
                else
                {
                    IsAdd = true;
                    infoENT.ContactInfoUID = infoBLL.InsertENT(infoENT);
                    hidInfoUID.Value = infoENT.ContactInfoUID.ToString();
                }
                //如果是添加，则修改CustomerENT的 InfoUID、AddressUID
                if (IsAdd)
                {
                    customerENT.CustomerUID = customeruid;
                    customerENT = customerBLL.GetEntity(customerENT);
                    customerENT.ContactInfoUID = infoENT.ContactInfoUID;
                    customerENT.AddressUID = addressENT.AddressUID;
                    intCustomer = customerBLL.UpDateENT(customerENT);
                }
                WriteToLog();
                try
                {
                    scope.Complete();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200004");
                }
                catch (Exception er)
                {
                    lblMeg.Text = ResourceManage.GetResourceString("msg200003");
                }
            }
        }
        #endregion

        protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCountry();
        }

        #region
        /// <summary>
        /// 
        /// </summary>
        protected void WriteToLog()
        {
            BasePage basepage = new BasePage();
            string ChangeLoge = string.Empty;
            string LoginName = basepage.GetManageFromSession().User_Name;
            #region 获取最新的数据判断更改了什么
            if (Val(staticeEnt.Area_Chi) != txtAreaChi.Text) { ChangeLoge += ", 地區 change to " + txtAreaChi.Text; }
            if (Val(staticeEnt.Area_Eng) != txtAreaEng.Text) { ChangeLoge += " , Area change to " + txtAreaEng.Text; }
            if (Val(staticeEnt.City_Chi) != txtCityChi.Text) { ChangeLoge += " , 城市 change to " + txtCityChi.Text; }
            if (Val(staticeEnt.City_Eng) != txtCityEng.Text) { ChangeLoge += " , City change to " + txtCityEng.Text; }
            if (Val(staticeEnt.District_Chi) != txtDistrictChi.Text) { ChangeLoge += " , 區域 change to " + txtDistrictChi.Text; }
            if (Val(staticeEnt.District_Eng) != txtDistrictEng.Text) { ChangeLoge += " , District change to " + txtDistrictEng.Text; }
            if (Val(staticeEnt.Flat_Bldg_Eng) != txtFlatEng.Text) { ChangeLoge += " , 室, 樓層, 大廈 change to " + txtFlatEng.Text; }
            if (Val(staticeEnt.Flat_Bldg_Chi) != txtFlatChi.Text) { ChangeLoge += " , Flat_Bldg change to " + txtFlatChi.Text; }
            if (Val(staticeEnt.Province_Chi) != txtProvinceChi.Text) { ChangeLoge += " , 州, 省 change to " + txtProvinceChi.Text; }
            if (Val(staticeEnt.Province_Eng) != txtProvinceEng.Text) { ChangeLoge += " , Province change to " + txtProvinceEng.Text; }
            if (Val(staticeEnt.Street_Rd_Chi )!= txtStreetChi.Text) { ChangeLoge += " , 街道 change to " + txtStreetChi.Text; }
            if (Val(staticeEnt.Street_Rd_Eng) != txtStreetEng.Text) { ChangeLoge += " , Street_Rd change to" + txtStreetEng.Text; }
            if (Val(staticeEnt.PostalCode) != txtPostCode.Text) { ChangeLoge += " , PostalCode change to " + txtPostCode.Text; }
            if (Val(staticeEnt.Department_Eng) != ddlArea.SelectedItem.Text) { ChangeLoge += " , Continent change to " + ddlArea.SelectedItem.Text; }
            if (Val(staticeEnt.Department_Chi) != ddlCountry.SelectedItem.Text) { ChangeLoge += " , Country change to" + ddlCountry.SelectedItem.Text; }
            ChangeLoge += method(ChangeLoge);

            #endregion
            Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
            tblHistory entlog = new tblHistory();
            entlog.HistoryCatUID = 2;
            entlog.HistoryTypeUID = 3;
            entlog.CustomerUID = Convert.ToInt32(Request.QueryString[Constant.UrlParamName.CustomerUID]);
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = LoginName + " {" + CompanyName.Text + "}{AddressContactInformation} " + ChangeLoge;
            entlog.LastModifyBy = LoginName;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        /// <summary>
        ///判断Tel Fax Email Website有没有更改
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string method(string str)
        {
            if (statictEntInfo != null)
            {
                tblContactInfo infoENT = new tblContactInfo();
                infoENT = GetInfoValue(infoENT);
                
                if (Val(statictEntInfo.Tel1) != Val(infoENT.Tel1) | Val(statictEntInfo.Tel2) != Val(infoENT.Tel2) | Val(statictEntInfo.Tel3) != Val(infoENT.Tel3) | Val(statictEntInfo.Tel4) != Val(infoENT.Tel4) | Val(statictEntInfo.Tel5) != Val(infoENT.Tel5))
                {
                    str += "Tel have change; ";
                }
                if (statictEntInfo.Tel1_IsDNC != infoENT.Tel1_IsDNC | statictEntInfo.Tel2_IsDNC != infoENT.Tel2_IsDNC | statictEntInfo.Tel3_IsDNC != infoENT.Tel3_IsDNC | statictEntInfo.Tel4_IsDNC != infoENT.Tel4_IsDNC | statictEntInfo.Tel5_IsDNC != infoENT.Tel5_IsDNC)
                {
                    str += "Tel DNC have change; ";
                }
                if (statictEntInfo.Tel1_IsSubscribe != infoENT.Tel1_IsSubscribe | statictEntInfo.Tel2_IsSubscribe != infoENT.Tel2_IsSubscribe | statictEntInfo.Tel3_IsSubscribe != infoENT.Tel3_IsSubscribe | statictEntInfo.Tel4_IsSubscribe != infoENT.Tel4_IsSubscribe | statictEntInfo.Tel5_IsSubscribe != infoENT.Tel5_IsSubscribe)
                {
                    str += "Tel Subscrib have change; ";
                }
                if (Val(statictEntInfo.Fax1) != Val(infoENT.Fax1) | Val(statictEntInfo.Fax2) != Val(infoENT.Fax2) | Val(statictEntInfo.Fax3) != Val(infoENT.Fax3) | Val(statictEntInfo.Fax4) != Val(infoENT.Fax4) | Val(statictEntInfo.Fax5) != Val(infoENT.Fax5))
                {
                    str += "Fax have change";
                }
                if (statictEntInfo.Fax1_IsDNC != infoENT.Fax1_IsDNC | statictEntInfo.Fax2_IsDNC != infoENT.Fax2_IsDNC | statictEntInfo.Fax3_IsDNC != infoENT.Fax3_IsDNC | statictEntInfo.Fax4_IsDNC != infoENT.Fax4_IsDNC | statictEntInfo.Fax5_IsDNC != infoENT.Fax5_IsDNC)
                {
                    str += "Fax DNC chave change";
                }
                if (statictEntInfo.Fax1_IsSubscribe != infoENT.Fax1_IsSubscribe | statictEntInfo.Fax2_IsSubscribe != infoENT.Fax2_IsSubscribe | statictEntInfo.Fax3_IsSubscribe != infoENT.Fax3_IsSubscribe | statictEntInfo.Fax4_IsSubscribe != infoENT.Fax4_IsSubscribe | statictEntInfo.Fax5_IsSubscribe != infoENT.Fax5_IsSubscribe)
                {
                    str += "Fax Subscribe have change";
                }
                if (Val(statictEntInfo.Email1) != Val(infoENT.Email1) | Val(statictEntInfo.Email2) != Val(infoENT.Email2) | Val(statictEntInfo.Email3) != Val(infoENT.Email3) | Val(statictEntInfo.Email4) != Val(infoENT.Email4) | Val(statictEntInfo.Email5) != Val(infoENT.Email5))
                {
                    str += "Email have change";
                }
                if (statictEntInfo.Email1_IsSubscribe != infoENT.Email1_IsSubscribe | statictEntInfo.Email2_IsSubscribe != infoENT.Email2_IsSubscribe | statictEntInfo.Email3_IsSubscribe != infoENT.Email3_IsSubscribe | statictEntInfo.Email4_IsSubscribe != infoENT.Email4_IsSubscribe | statictEntInfo.Email5_IsSubscribe != infoENT.Email5_IsSubscribe)
                {
                    str += "Email Subscribe have change";
                }
                if (statictEntInfo.Email1_HardBounce != infoENT.Email1_HardBounce | statictEntInfo.Email2_HardBounce != infoENT.Email2_HardBounce | statictEntInfo.Email3_HardBounce != infoENT.Email3_HardBounce | statictEntInfo.Email4_HardBounce != infoENT.Email4_HardBounce | statictEntInfo.Email5_HardBounce != infoENT.Email5_HardBounce)
                {
                    str += "Email Subscribe have change";
                }
                if (statictEntInfo.Website1 != infoENT.Website1 | statictEntInfo.Website2 != infoENT.Website2 | statictEntInfo.Website3 != infoENT.Website3 | statictEntInfo.Website4 != infoENT.Website4 | statictEntInfo.Website5 != infoENT.Website5)
                {
                    str += "Website have change";
                }
                if (statictEntInfo.Website1_Type != infoENT.Website1_Type | statictEntInfo.Website2_Type != infoENT.Website2_Type)
                {
                    str += "Website type have change";
                }
            }
            return str;
        }

        protected string Val(string str)
        {
            if (string.IsNullOrEmpty(str))
                str = "";
            return str;
        }
        #endregion        
    }
}